package com.beizhen.demo.config;


import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.baomidou.mybatisplus.extension.api.R;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 孟希
 * 和数据库交互，看看用户信息，权限信息
 */
@Configuration
public class ShiroConfig {
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactory(@Qualifier("securityManager")SecurityManager securityManager){
        //控制 访问xx资源 需要xx权限
        ShiroFilterFactoryBean shiroFilterFactoryBean=new ShiroFilterFactoryBean();
       shiroFilterFactoryBean.setSecurityManager(securityManager);
        shiroFilterFactoryBean.setLoginUrl("/stuUser/toLogin");
          System.out.println("执行了");
        Map filterChainMap=new  LinkedHashMap ();
        filterChainMap.put("/static/**/**","anon");//访问登陆
        filterChainMap.put("/css/**","anon");//访问登陆
        filterChainMap.put("/js/**","anon");//访问登陆
        filterChainMap.put("/img/**","anon");//访问登陆
        /*filterChainMap.put("/stuUser/toLogin","anon");
        filterChainMap.put("/stuUser/doLogin","anon");*/

        // 页面，直接放行


        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainMap);
        return shiroFilterFactoryBean;
    }

   //安全管理器
    @Bean
    public DefaultWebSecurityManager securityManager(){
        System.out.println("myRealm");
        DefaultWebSecurityManager securityManager=new DefaultWebSecurityManager();
        securityManager.setRealm(myRealm());
        return securityManager;
    }

    @Bean
    public Realm myRealm(){
        MyRealm myRealm=new MyRealm();
        //告诉密码的匹配放方式k
        myRealm.setCredentialsMatcher(credentialsMatcher());

        return myRealm;
    }

    @Bean
    public CredentialsMatcher credentialsMatcher(){
        System.out.println("md5");
        HashedCredentialsMatcher hashedMatcher=new HashedCredentialsMatcher();
        hashedMatcher.setHashAlgorithmName("md5");
        return hashedMatcher;
    }

    /**
     * 注解支持
     */
    @Bean
    @ConditionalOnMissingBean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator defaultAAP = new DefaultAdvisorAutoProxyCreator();
        defaultAAP.setProxyTargetClass(true);
        return defaultAAP;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

    @Bean
    public ShiroDialect shiroDialect(){
        return new ShiroDialect();
    }
}
